package pl.squeaky.mvc.wizard;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;

import pl.squeaky.mvc.generator.ModelGenerator;
import pl.squeaky.mvc.nature.MvcNature;
import pl.squeaky.mvc.util.IDEUtils;
import pl.squeaky.mvc.util.SelectionUtils;

public class NewModelWizard extends Wizard implements INewWizard {

	private IStructuredSelection selection;

	public NewModelWizard() {
		setWindowTitle("New model");
	}

	@Override
	public boolean performFinish() {
		MvcNature nature = SelectionUtils.getMvcNature(selection);
		if(nature != null) {
			IFolder modelsFolder = nature.getModels().getPath();
			NewModelNamePage namePage = (NewModelNamePage)getPage("NewModelNamePage");
			
			ModelGenerator generator = new ModelGenerator(namePage.getModelName());
			generator.setSuperclass(namePage.getSuperclassName());
			
			IFile file = generator.generate(modelsFolder, namePage.getModelName());
			
			IDEUtils.open(file);
		}
		
		return true;
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = selection;
	}

	@Override
	public void addPages() {
		addPage(new NewModelNamePage());
	}
}
